import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useAppStore = defineStore('app', () => {
  // 侧边栏折叠状态
  const sidebarCollapsed = ref(false)

  // 加载状态
  const loading = ref(false)

  // 系统配置
  const systemConfig = ref({
    siteName: '乒乓球培训管理系统',
    version: '1.0.0',
    theme: 'orange', // 橙色主题
    language: 'zh-CN'
  })

  // 消息通知
  const notifications = ref<Array<{
    id: string
    type: 'info' | 'success' | 'warning' | 'error'
    title: string
    message: string
    timestamp: Date
    read: boolean
  }>>([])

  // 切换侧边栏
  const toggleSidebar = () => {
    sidebarCollapsed.value = !sidebarCollapsed.value
  }

  // 设置加载状态
  const setLoading = (status: boolean) => {
    loading.value = status
  }

  // 添加通知
  const addNotification = (notification: {
    type: 'info' | 'success' | 'warning' | 'error'
    title: string
    message: string
  }) => {
    const id = Date.now().toString()
    notifications.value.unshift({
      id,
      ...notification,
      timestamp: new Date(),
      read: false
    })

    // 限制通知数量
    if (notifications.value.length > 50) {
      notifications.value = notifications.value.slice(0, 50)
    }
  }

  // 标记通知为已读
  const markNotificationAsRead = (id: string) => {
    const notification = notifications.value.find(n => n.id === id)
    if (notification) {
      notification.read = true
    }
  }

  // 标记所有通知为已读
  const markAllNotificationsAsRead = () => {
    notifications.value.forEach(n => n.read = true)
  }

  // 删除通知
  const removeNotification = (id: string) => {
    const index = notifications.value.findIndex(n => n.id === id)
    if (index > -1) {
      notifications.value.splice(index, 1)
    }
  }

  // 清空所有通知
  const clearAllNotifications = () => {
    notifications.value = []
  }

  // 获取未读通知数量
  const unreadNotificationCount = () => {
    return notifications.value.filter(n => !n.read).length
  }

  // 初始化一些默认通知
  const initDefaultNotifications = () => {
    addNotification({
      type: 'info',
      title: '欢迎使用',
      message: '欢迎使用乒乓球培训管理系统！'
    })
    
    addNotification({
      type: 'success',
      title: '系统更新',
      message: '系统已更新到最新版本 v1.0.0'
    })
  }

  return {
    sidebarCollapsed,
    loading,
    systemConfig,
    notifications,
    toggleSidebar,
    setLoading,
    addNotification,
    markNotificationAsRead,
    markAllNotificationsAsRead,
    removeNotification,
    clearAllNotifications,
    unreadNotificationCount,
    initDefaultNotifications
  }
})
